/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Model.JConexion;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 *
 * @author Juan Garza
 */
public class C_Apuesta_individual extends HttpServlet{
    String query = "";
    String[] idcaballoS;
    String apuesta ="";
    String[] posicion;
    String idcarreraS = "";
    String user = "";

    int idCaballo = 0;
    int pos = 0;
    float cantidad = 0;
    int idcarrera = 0;
    int iduser = 0;
    JConexion jc = new JConexion("");

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, InstantiationException, IllegalAccessException, NullPointerException, NumberFormatException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            this.idcaballoS = request.getParameterValues("idcaballo");
            this.apuesta = request.getParameter("apuesta");
            this.posicion = request.getParameterValues("posicion");
            this.idcarreraS = request.getParameter("idcarrera");
            this.user = request.getParameter("user");

            //System.out.println("idcarreraS: " + this.idcarreraS);

            for(int i=0; i<1; i++){
            this.pos = Integer.parseInt(this.posicion[i].toString());
            this.cantidad = Float.parseFloat(this.apuesta);
            //this.idCaballo = Integer.parseInt(this.idcaballoS[i].toString().substring(0,this.idcaballoS[i].toString().length()-2));
            this.idCaballo = Integer.parseInt(this.idcaballoS[i].toString().substring(0,this.idcaballoS[i].toString().length()-2));
            this.idcarrera = Integer.parseInt(this.idcarreraS);
            this.iduser = Integer.parseInt(this.user);
            }
            //System.out.println(this.idcaballoS[0].toString().length());

            this.query = "Insert Into royal_mowes.apuesta(fk_id_carrera, fk_id_usuario, cantidad, fk_id_posicion, fk_id_caballo) Values("+ this.idcarrera + ", " + this.iduser + ", " + this.cantidad + ", " + this.pos + ", " + this.idCaballo +")";
            System.out.println(this.query);

            jc.MySQLArray("MySQLRM", "Insert", this.query);

            out.println("<html><head><title>Redireccionado</title></head>"
                        + "<body><center><br><br><big><big><font face=\"verdana\">Apuesta Registrada</font></big></big><br><br><br><br>"
                        + "<a href=\"http://"+ request.getLocalName() + ":" + request.getLocalPort() +"/RoyalMowes/caballo_carrera.jsp\">Volver a apostar</a>"
                        + "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"http://"+ request.getLocalName() + ":" + request.getLocalPort() +"/RoyalMowes/login.jsp\">Paginal Principal</a></center></body></html>");

            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet C_RelCarrera</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet C_RelCarrera at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (InstantiationException ex) {
            Logger.getLogger(C_RelCarrera.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(C_RelCarrera.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (InstantiationException ex) {
            Logger.getLogger(C_RelCarrera.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(C_RelCarrera.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
